package com.atguigu.hadoop.examples.mapreduce.wordStandardDeviation;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

public class WordStandardDeviationMapper extends Mapper<Object, Text, Text, LongWritable> {
    private LongWritable wordLen = new LongWritable();
    private LongWritable wordLenSq = new LongWritable();

    public void map(Object key, Text value, Mapper<Object, Text, Text, LongWritable>.Context context)
            throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            String string = itr.nextToken();

            this.wordLen.set(string.length());

            //Math.pow() 函数返回基数（base）的指数（exponent）次幂，即 baseexponent。
            Double nums = Math.pow(Double.valueOf(string.length()), 2.0D);

            this.wordLenSq.set(nums.longValue());

            context.write(WordStandardDeviation.LENGTH, this.wordLen);
            context.write(WordStandardDeviation.SQUARE, this.wordLenSq);
            context.write(WordStandardDeviation.COUNT, WordStandardDeviation.ONE);
        }
    }
}